package org.nulldevice.tankshot.entities;

import org.nulldevice.tankshot.GameContainer;
import org.nulldevice.tankshot.images.ImageContainer;

public class Mine extends Bullet {

	private static int mineOffsetX = 6;
	private static int mineOffsetY = 6;
	private long mineStarted=0;
	private long mineExplosionTime = 8000;
	private boolean aboutToDetonate=false;
	
	public Mine(Tank owner, int posX, int posY) {
		super(owner, posX, posY, 0, 0, 12);
		this.sizeX = 12;
		this.sizeY = 12;
		this.mineStarted=System.currentTimeMillis();
		imagerepresentant = ImageContainer.get().getImagerepresentant(
				"mine.png", mineOffsetX, mineOffsetY);
	}


	@Override
	public void hitItem(Item item) {		
		super.hitItem(item);
		// mines do not explode immediatelly, they get bigger fist
		// mines can destroy more than one item
		if (isAboutToExplode()==true){
			aboutToExplode=false;
			if (!aboutToDetonate)
				mineIsAboutToDetonate();
		}
	}

	@Override
	public boolean move(double delta) {
		if (deadlyForOwner == false) {
			if (!GameContainer.getInstance().isCollisions(this, owner)) {
				deadlyForOwner = true;
			}
		}
		
		// after the explosion is prepared, it explodes here
		if (aboutToDetonate==true){
			aboutToExplode=true;
		}else{
			// Check if the timer of the mine is about to explode
			if (System.currentTimeMillis()-mineStarted-mineExplosionTime>0){
				mineIsAboutToDetonate();
				return true;
			}
		}

		// System.out.println("Mine: "+getX()+" "+getY());

		// TODO Add explosion counter here
		return false;
	}

	private void mineIsAboutToDetonate(){
		deadlyForOwner=true;
		aboutToDetonate=true;
		sizeX *= 4;
		sizeY *= 4;
		recalcBorder=true;
	}
}
